package com.sun.app.service.app;

import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import sun.core.json.JSONArray;
import sun.core.json.JSONObject;

import com.sun.app.service.BaseService;
import com.sun.util.AppResult;

public class ComboService extends BaseService {

	private static final String QUERY_PROVINCE = "SELECT CODE,PROVINCESNAME FROM m_provinces";

	public List getProvice() {
		return jt.queryForList(QUERY_PROVINCE);
	}
	
	private static final String QUERY_TIP="select TIPNAME,TIPID from c_tip where PARENTID=0";
	public List getTip(){
		return jt.queryForList(QUERY_TIP);
	}

	// 初始化用户学校
	private static final String QUERY_SCHOOLNAME_BY_ID = "select schoolname from m_school where schoolid = ?;";

	public AppResult userInitSchool(HttpServletRequest request) {
		String schoolid = request.getParameter("schoolid");
		List queryForList = jt.queryForList(QUERY_SCHOOLNAME_BY_ID,schoolid);
		if (!queryForList.isEmpty()) {
			rs.setStatus(200);
			rs.setResult(queryForList.get(0));
			return rs;
		}
		rs.setStatus(300);
		return rs;
	}

	// 刷新学校

	private static final String QUERY_SCHOOL_BY_CODE = "SELECT SCHOOLID,SCHOOLNAME from m_school where code = ?";

	public AppResult refreshSchool(HttpServletRequest request) {
		String code = request.getParameter("code");
		List schools = jt.queryForList(QUERY_SCHOOL_BY_CODE, code);
		ListIterator it = schools.listIterator();
		JSONArray a = new JSONArray();
		while (it.hasNext()) {
			Map map = (Map) it.next();
			JSONObject jo = new JSONObject();
			jo.put("SCHOOLID", map.get("SCHOOLID").toString());
			jo.put("SCHOOLNAME", map.get("SCHOOLNAME").toString());
			a.add(jo);
		}
		rs.setResult(a);
		rs.setTotleCount(schools.size());
		return rs;
	}

	private static final String QUERY_CITY_BY_CODE = "SELECT CODE,PROVINCESCODE,CITYNAME FROM m_city WHERE PROVINCESCODE = ?";

	public AppResult refreshCity(HttpServletRequest request) {
		String code = request.getParameter("code");
		List city = jt.queryForList(QUERY_CITY_BY_CODE, code);
		ListIterator it = city.listIterator();
		JSONArray a = new JSONArray();
		while (it.hasNext()) {
			Map map = (Map) it.next();
			JSONObject jo = new JSONObject();
			jo.put("CODE", map.get("CODE").toString());
			jo.put("PROVINCESCODE", map.get("PROVINCESCODE").toString());
			jo.put("CITYNAME", map.get("CITYNAME"));
			a.add(jo);
		}
		System.out.println(a.toString());
		rs.setResult(a);
		rs.setTotleCount(city.size());
		return rs;
	}
	
	private static final String QUERY_TIP_BY_TIPID =  "select TIPID,TIPNAME from c_tip where PARENTID = ?";
	public AppResult refreshTip(HttpServletRequest request) {
		String code = request.getParameter("code");
		List tip = jt.queryForList(QUERY_TIP_BY_TIPID, code);
		ListIterator it = tip.listIterator();
		JSONArray a = new JSONArray();
		while (it.hasNext()) {
			Map map = (Map) it.next();
			JSONObject jo = new JSONObject();
			jo.put("CODE", map.get("TIPID").toString());
			jo.put("TIPNAME", map.get("TIPNAME"));
			a.add(jo);
		}
		System.out.println(a.toString());
		rs.setResult(a);
		rs.setTotleCount(tip.size());
		return rs;
	}

	@Override
	public Integer add(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer delete(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer update(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}

}
